﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms
{
    public partial class frmTransfer : Form
    {
        
        // Constructor
        public frmTransfer()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form Load
        private void frmTransfer_Load(object sender, EventArgs e)
        {
            // Fill TreeViews
            DisciplineListHelper.FillTreeView(tvSource);
            DisciplineListHelper.FillTreeView(tvTarget);

            if (tvSource.Nodes.Count > 0)
            {
                tvSource.SelectedNode = tvSource.Nodes[0];
            }
        }

        // Source TreeView select
        private void tvSource_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvSource.SelectedNode != null)
            {
                this.txtFrom.Text = tvSource.SelectedNode.FullPath;
                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Convert.ToInt32(tvSource.SelectedNode.Name));
            }
        }

        // Target TreeView select
        private void tvTarget_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvTarget.SelectedNode != null)
            {
                this.txtTo.Text = tvTarget.SelectedNode.FullPath;
            }
        }

        // Transfer
        private void tsbTransfer_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.bsPerformances.EndEdit();
                
                int count = 0;

                foreach (DataGridViewRow dgvr in dgvPerformances.Rows)
                {
                    if (Convert.ToBoolean(dgvr.Cells["chk"].Value))
                    {
                        DataRowView drv = (DataRowView)dgvr.DataBoundItem;
                        DataRow dr = (DataRow)drv.Row;
                        dr.SetModified();
                        
                        dr["DisciplineListId"] = Convert.ToInt32(tvTarget.SelectedNode.Name);
                        
                        this.taPerformances.Update(dr);
                        count += 1;
                    }
                }

                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Convert.ToInt32(tvSource.SelectedNode.Name));
                MessageBox.Show("Pretekár(i) boli presunutí." + Environment.NewLine + "Počet: " + count.ToString(), "Presuny", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Presuny", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Copy
        private void tsbCopy_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.bsPerformances.EndEdit();
                
                int count = 0;

                foreach (DataGridViewRow dgvr in dgvPerformances.Rows)
                {
                    if (Convert.ToBoolean(dgvr.Cells["chk"].Value))
                    {
                        DataRowView drv = (DataRowView)dgvr.DataBoundItem;
                        DataRow dr = (DataRow)drv.Row;
                        dr.SetAdded();

                        // Reset sync params on copy
                        dr["DisciplineListId"] = Convert.ToInt32(tvTarget.SelectedNode.Name);
                        dr["WebId"] = 0;
                        dr["Sync"] = false;

                        this.taPerformances.Update(dr);
                        count += 1;
                    }
                }

                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Convert.ToInt32(tvSource.SelectedNode.Name));
                MessageBox.Show("Pretekár(i) boli prekopírovaní." + Environment.NewLine + "Počet: " + count.ToString(), "Presuny", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Presuny", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Check all
        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dgvPerformances.Rows)
            {
                dgvr.Cells["chk"].Value = chkAll.Checked;
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
